package cn.seven._conf._jfinal;


import java.sql.Connection;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.aop.InterceptorManager;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.tx.TxByMethodRegex;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.source.ClassPathSourceFactory;

/**
 * create by 张天笑, 2017/11/1 23:37
 */
@Configuration
public class DBConfig {

    private static Boolean alreadyConfig = false;

    public static DruidPlugin getDruidPlugin() {
        //加载德鲁伊插件
        DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("url"), PropKit.get("username"), PropKit.get("password"),
                PropKit.get("driverClassName"));
        WallFilter wallFilter = new WallFilter();
        wallFilter.setDbType(JdbcConstants.POSTGRESQL);
        druidPlugin.addFilter(wallFilter);// 加强数据库安全,防御SQL注入攻击
        druidPlugin.addFilter(new StatFilter());// 添加 StatFilter 才会有统计数据
        druidPlugin.setLogAbandoned(true);
        return druidPlugin;
    }

    @Bean
    public DataSource dataSource() {
       /* DruidPlugin druidPlugin = getDruidPlugin();
        druidPlugin.start();
        DataSource dataSource = getDruidPlugin().getDataSource();
        return dataSource;*/
        DruidPlugin druidPlugin = getDruidPlugin();
        druidPlugin.start();
        DataSource dataSource = druidPlugin.getDataSource();
        return dataSource;
    }

    public static void startArp() throws Exception {
        Boolean devModel = PropKit.getBoolean("engineDevMode");
        DruidPlugin druidPlugin = getDruidPlugin();
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);

        //arp.setDevMode(devModel);
        arp.getEngine().setDevMode(devModel);

        //arp.setShowSql(true);执行sql时在控制台打印
		arp.setDialect(new MysqlDialect());
        //arp.setDialect(new CompatibleDialect());//设置方言
        //arp.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大小写
		
		arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
        arp.setTransactionLevel(Connection.TRANSACTION_READ_COMMITTED);//设置隔离级别位读已提交
        arp.setBaseSqlTemplatePath(null);//设置sql模板路径
        arp.addSqlTemplate("sql/me.sql");

        //方法名匹配正则时开启事物
        InterceptorManager.me().addGlobalServiceInterceptor(new TxByMethodRegex("insert.*|save.*|add.*|update.*|del.*"));


        //arp.setPrimaryKey("document", "mainMenu,subMenu");// 强制指定复合主键的次序，避免不同的开发环境生成在 _MappingKit 中的复合主键次序不相同
        druidPlugin.start();
        //添加Model映射
        try {
            //_MappingKit.mapping(arp);
        } catch (Exception e) {
            throw new Exception("activerecord mapping 报错");
        }

        //启动ActiveRecord插件
        try {
            if (!alreadyConfig) {
                arp.start();
                alreadyConfig = true;
            }
        } catch (Exception e) {
            throw e;
        }
    }
}
